//
//  XinxinTools.m
//  hong5
//
//  Created by 新新 on 2017/6/28.
//  Copyright © 2017年 com.hong5.ios. All rights reserved.
//

#import "XinxinTools.h"

@implementation XinxinTools

+ (float) heightForString:(UILabel *)label andWidth:(float)width{
    CGSize sizeToFit = [label sizeThatFits:CGSizeMake(width, MAXFLOAT)];
    return sizeToFit.height;
}

+ (CGSize)calculateRowWidth:(NSString *)string font:(UIFont *)font width:(CGFloat)weight {
    NSDictionary *dic = @{NSFontAttributeName:font};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(weight, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size;
}

+ (id)toArrayOrNSDictionary:(NSString *)jsonData{
    
    jsonData = [jsonData stringByReplacingOccurrencesOfString:@"    " withString:@""];
    jsonData = [jsonData stringByReplacingOccurrencesOfString:@"<a>" withString:@""];
    jsonData = [jsonData stringByReplacingOccurrencesOfString:@"<\\/a>" withString:@""];
    jsonData = [jsonData stringByReplacingOccurrencesOfString:@"(\\\\)"
                                                   withString:@"\\\\\\\\" options:NSRegularExpressionSearch
                                                        range:NSMakeRange(0, [jsonData length])];
    jsonData = [jsonData stringByReplacingOccurrencesOfString:@"(\r)"
                                                   withString:@"\\\\r" options:NSRegularExpressionSearch
                                                        range:NSMakeRange(0, [jsonData length])];
    jsonData = [jsonData stringByReplacingOccurrencesOfString:@"(\n)"
                                                   withString:@"\\\\n" options:NSRegularExpressionSearch
                                                        range:NSMakeRange(0, [jsonData length])];
    NSData * data = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    id any = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    return any;
}


+ (UIViewController *)superViewController:(id)view
{
    for (UIView* next = [view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


+ (NSString *)playNoStr:(NSInteger)playNo
{
    NSInteger playNoNumber = playNo;
    NSString *playNoStr = [NSString stringWithFormat:@"%ld次",(long)playNoNumber];
    if (playNoNumber > 10000) {
        playNoStr = [NSString stringWithFormat:@"%.2f万次",playNoNumber/10000.0];
    }
    if (playNoNumber > 100000000) {
        playNoStr = [NSString stringWithFormat:@"%.2f亿次",playNoNumber/100000000.0];
    }
    if (playNoNumber < 10000) {
        playNoStr = [NSString stringWithFormat:@"%.1f万次",ceil(playNoNumber/1000.0)/10];
    }
    return playNoStr;
}

+ (UIImage *)QRCodeImageWithString:(NSString *)content size:(CGSize)imageSize {
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复默认设置
    [filter setDefaults];
    
    // 3. 给过滤器添加数据
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    // 注意，这里的value必须是NSData类型
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 4. 生成二维码
    CIImage *outputImage = [filter outputImage];
    
    CGRect extent = CGRectIntegral(outputImage.extent);
    CGFloat scale = MIN(imageSize.width/CGRectGetWidth(extent), imageSize.height/CGRectGetHeight(extent));
    
    // 1. 创建bitmap
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:outputImage fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

@end
